﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PatentFee.Entity;

namespace PatentFee.Bussines
{
    /// <summary>
    /// 申请费信息操作类。
    /// </summary>
    public class ApplicationFeeInfoBussines
    {
        /// <summary>
        /// 新增信息。
        /// </summary>
        /// <param name="applicationFeeInfo">申请费信息。</param>
        public void Add(ApplicationFeeInfo applicationFeeInfo)
        {
            using (var dbContext = new PatentFeeDbContext())
            {
                dbContext.ApplicationFeeInfos.Add(applicationFeeInfo);
                dbContext.SaveChanges();
            }
        }

        /// <summary>
        /// 修改信息。
        /// </summary>
        /// <param name="applicationFeeInfo">申请费信息。</param>
        public void Change(ApplicationFeeInfo applicationFeeInfo)
        {
            using (var dbContext = new PatentFeeDbContext())
            {
                var entity = dbContext.ApplicationFeeInfos.FirstOrDefault(p => p.Id == applicationFeeInfo.Id);
                if (entity != null)
                {
                    entity = applicationFeeInfo;
                    dbContext.SaveChanges();
                }
            }
        }

        /// <summary>
        /// 删除信息。
        /// </summary>
        /// <param name="id">申请费信息id。</param>
        public void Remove(int id)
        {
            using (var dbContext = new PatentFeeDbContext())
            {
                var entity = dbContext.ApplicationFeeInfos.FirstOrDefault(p => p.Id == id);
                if (entity != null)
                {
                    dbContext.ApplicationFeeInfos.Remove(entity);
                    dbContext.SaveChanges();
                }
            }
        }

        /// <summary>
        /// 根据id获取信息。
        /// </summary>
        /// <param name="id">申请费id。</param>
        /// <returns>返回对应id的信息，如果没有则返回null。</returns>
        public ApplicationFeeInfo GetApplicaitonFeeById(int id)
        {
            using (var dbContext = new PatentFeeDbContext())
            {
                return dbContext.ApplicationFeeInfos.FirstOrDefault(p => p.Id == id);
            }
        }
    }
}
